package com.itheima.interceptor;

import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Objects;

@Component
public class LoginInterceptor implements HandlerInterceptor {


    /*****
     * 用户登录拦截器
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1）获取请求头中的令牌
        String token = request.getHeader("token");

        //2)如果令牌为空，或者令牌解析失败，则拦截请求
        if(Objects.isNull(token) || !JwtUtils.verfiy(token)){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
